define(function (require, exports, module) {

var Alert = require('alert');

var NarrowForm = require('../common/form/narrowform')/*,
    sha1 = require('common/sha1')*/;

var myUrl = '/admin/my.html?' + new Date().getTime();

if (window.GLOBAL.USER_CODE) {
  window.location.href = myUrl;
} else {
  new NarrowForm({
    container: '#loginform',
    url: 'login',
    customRules: {
      name: {
        chkname: function (e) {
          return !e.value ||
                /^[a-z][0-9a-z]+$/i.test(e.value);
        }
      }
    },
    customMessages: {
      name: {
        chkname: '用户名只能包含字母与数字，且以字母开头',
        required: '请输入用户名'
      },
      password: {
        required: '请输入密码'
      }
    },
    data: {
      groups: [{
        colspan: 12,
        attrs: {
          type: 'text',
          name: 'name',
          placeholder: '用户名',
          autofocus: 'autofocus',
          value: 'cy5831',
          required: 'required',
          maxlength: 50,
          chkname: 'chkname'
        }
      }, {
        colspan: 12,
        attrs: {
          type: 'password',
          name: 'pwd',
          value: '',
          placeholder: '密码',
          required: 'required',
          maxlength: 50,
          onpaste: 'return false'
        }
      }, {
        colspan: 12,
        value: '登录',
        attrs: {
          type: 'submit',
          'class': 'btn btn-primary'
        }
      }]
    },
    events: {
      // formData: function (e) {
      //   var formData = e.target.formData;
      //   formData.set('password', sha1(formData.get('password')));
      // },
      done: function (e, data) {
        if (data.data === true) {
          window.location.href = myUrl;
        } else {
          new Alert({
            content: (data && data.messages) || '登录异常'
          });
        }
      },
      fail: function (e, data) {
        new Alert({
          content: (data && data.messages) || '登录异常'
        });
      }
    }
  });
}

});
